<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>云校通-教师-我的班级</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<script src="<%=basePath %>/js/picker.js"></script>
<script src="<%=basePath %>/js/select.js"></script>
<body ontouchstart id="wd_xkpt">
	<div class="weui_tab tab-bottom">
		<div class="weui_tab_bd">
			<div class="weui-header bg-blue"> 
		  		<div class="weui-header-left"> <a href="<%=basePath %>/ls/kccs/course" class="icon icon-109 f-white">返回</a> </div>
		    	<h1 class="weui-header-title">${term.termName }</h1>
		    </div>
	        <div class="weui_tab" id="tab4">
	            <div class="weui_navbar">
	                <div class="weui_navbar_item ">
	                   	 已报名<span id="hadCount"></span>
	                </div>
	                <div class="weui_navbar_item">
	                   	 未报名<span id="noCount"></span>
	                </div>
	            </div>
	            <div class="weui_tab_bd" style="padding-bottom: 55px;">
			        <div class="weui_tab_bd_item">
			        	<div class="weui_panel_bd">
	                    	<div class="weui_media_box weui_media_small_appmsg" id="signedList">
			                    
			                </div>  
			            </div>
			        </div>
			        <div class="weui_tab_bd_item">
			        	<div class="weui_panel_bd">
							<div class="weui_media_box weui_media_small_appmsg" id="noSignList">
	                    
	                		</div>
			            </div>
			        </div>
	            </div>
	        </div>
		</div>
	</div>
</body>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script type="text/javascript">
$(function() {
	$('#tab4').tab({
		defaultIndex: 0,
		activeClass: "tab-blue "
	});
	loadSignedList();
	loadNoSignList();
})

function loadSignedList() {
	$.ajax({
   		url: basePath + "/ls/kccs/course/signed/list",
   		type: 'POST',
   		dataType : "json",
   		success: function(data) {
   			if(data != null) {
   				var html = '';
   				for(var i in data) {
   					html += '<div class="weui_cells weui_cells_1">\
								<div class="weui_cell">\
							<div class="weui_cell_bd weui_cell_primary">\
								<p>'+data[i].studentName+'</p>\
							</div>\
							<div class="weui_cell_ft">\
								<span class="icon icon-99 f20 f-blue"></span>\
								<a href="javascript:;" onclick="changeClass(this)">调班</a>\
							</div>\
						</div>\
						<div class="weui_cell xkpt-lesson">\
							<div class="weui_cell_bd weui_cell_primary">\
								<p>课程名称</p>\
							</div>\
							<div class="weui_cell_ft">'+data[i].courseName+'</div>\
							<input type="text" student-id="'+data[0].studentId+'" data-id="'+data[0].courseId+'" id="changeCourse_'+i+'" class="xkpt-lesson-input" readonly>\
						</div>\
					</div>';
   				}
   				$("#hadCount").html("("+data.length+")");
   				$("#signedList").html(html);
   				initChangeClass();
   			}
   		},
   		error: function(data) {
   			alert("系统异常,请联系管理员");
   		}
   });
}

function loadNoSignList() {
	$.ajax({
   		url: basePath + "/ls/kccs/course/noSign/list",
   		type: 'POST',
   		dataType : "json",
   		success: function(data) {
   			if(data != null) {
   				var html = '';
   				for(var i in data) {
   					html += '<div class="weui_cells weui_cells_1">\
								<div class="weui_cell">\
							<div class="weui_cell_bd weui_cell_primary">\
								<p>'+data[i].studentName+'</p>\
							</div>\
							<div class="weui_cell_ft">\
								<span class="icon icon-99 f20 f-blue"></span>\
								<a href="javascript:;" onclick="chooseClass(this)">选课</a>\
							</div>\
						</div>\
						<div class="weui_cell xkpt-lesson">\
							<div class="weui_cell_bd weui_cell_primary">\
								<p>课程名称</p>\
							</div>\
							<div class="weui_cell_ft">无</div>\
							<input type="text" student-id="'+data[0].studentId+'" id="chooseCourse_'+i+'" class="xkpt-lesson-input" readonly>\
						</div>\
					</div>';
   				}
   				$("#noCount").html("("+data.length+")");
   				$("#noSignList").html(html);
   				initChooseCourse();
   			}
   		},
   		error: function(data) {
   			alert("系统异常,请联系管理员");
   		}
   });
}
function initChangeClass() {
	$.ajax({
   		url: basePath + "/ls/kccs/course/list/query",
   		type: 'POST',
   		dataType : "json",
   		success: function(data) {
   			$("#signedList").find("input").each(function(i, v) {
   				var courseId = $(v).attr("data-id");
   				var values = [];
				var displayValues = [];
   				for(var i in data) {
   					if(data[i].id != courseId) {
   						values.push(data[i].id);
   						displayValues.push(data[i].courseName);
   					}
   				}
				$("#" + $(v)[0].id).picker({
   			        title: "请选择课程名称",
   			        toolbarCloseText:'确定',
   			        onClose:function(){
   			        	$(this.input).closest(".xkpt-lesson").find(".weui_cell_ft").text(this.cols[0].displayValue);
   			        },
   			        cols: [
   			          {
   			            textAlign: 'center',
   			            values: values,
   			            displayValues: displayValues,
   			          }
   			        ]
   			    });
   			})
   		},
   		error: function(data) {
   			alert("系统异常,请联系管理员");
   		}
	});
}
function initChooseCourse() {
	$.ajax({
   		url: basePath + "/ls/kccs/course/list/query",
   		type: 'POST',
   		dataType : "json",
   		success: function(data) {
   			var values = [];
   			var displayValues = [];
   			for(var i in data) {
   				values.push(data[i].id);
   				displayValues.push(data[i].courseName);
   			}
   			$("#noSignList").find("input").each(function(i, v) {
   				$("#" + $(v)[0].id).picker({
   			        title: "请选择课程名称",
   			        toolbarCloseText:'确定',
   			        onClose:function(){
   			        	$(this.input).closest(".xkpt-lesson").find(".weui_cell_ft").text(this.cols[0].displayValue);
   			        },
   			        cols: [
   			          {
   			            textAlign: 'center',
   			            values: values,
   			            displayValues: displayValues,
   			          }
   			        ]
   			    });
   			})
   		},
   		error: function(data) {
   			alert("系统异常,请联系管理员");
   		}
	});
}
function chooseClass(thiz) {
	var obj = $(thiz);
	var objOuter = obj.closest(".weui_cells");
	if(obj.hasClass("isChange")){
		var objInput = objOuter.find("input")
		var studentId = emptyToNull($(objInput).attr("student-id"));
		var courseId = emptyToNull($(objInput).val());
		if(studentId == null || courseId == null) return;
		$.ajax({
			url: basePath + "/ls/kccs/course/signSubmit",
	   		type: 'POST',
	   		dataType : "json",
	   		data: {
	   			studentId: studentId,
	   			courseId: courseId
	   		},
	   		success: function(data) {
	   			if(data.code == '000') {
	   				$.toast("提交成功");
	   				setTimeout(function() {
	   					loadNoSignList();
	   					loadSignedList();
    				}, 1000);
	   			}else {
	   				$.toast(data.msg, "cancel");
	   			}
	   		},
	   		error: function(data) {
	   			alert("系统异常,请联系管理员");
	   		}
		});
	}else{
		obj.addClass("isChange");
		obj.text("保存");
		objOuter.find(".xkpt-lesson").addClass("weui_cells_access");
		objOuter.find(".xkpt-lesson-input").addClass("on");
	}
}
function changeClass(thiz) {
	var obj = $(thiz);
	var objOuter = obj.closest(".weui_cells");
	if(obj.hasClass("isChange")){
		var objInput = objOuter.find("input")
		var studentId = emptyToNull($(objInput).attr("student-id"));
		var oldCourseId = emptyToNull($(objInput).attr("data-id"));
		var newCourseId = emptyToNull($(objInput).val());
		if(studentId == null || oldCourseId == null || newCourseId == null) return;
		$.ajax({
			url: basePath + "/ls/kccs/course/changeSubmit",
	   		type: 'POST',
	   		dataType : "json",
	   		data: {
	   			studentId: studentId,
	   			oldCourseId: oldCourseId,
	   			newCourseId: newCourseId,
	   		},
	   		success: function(data) {
	   			if(data.code == '000') {
	   				obj.text("调班");
		   			obj.removeClass("isChange");
		   			objOuter.find(".xkpt-lesson").removeClass("weui_cells_access");
		   			objOuter.find(".xkpt-lesson-input").removeClass("on");
	   				$.toast("提交成功");
	   				loadSignedList();
	   			}else {
	   				$.toast(data.msg, "cancel");
	   			}
	   		},
	   		error: function(data) {
	   			alert("系统异常,请联系管理员");
	   		}
		});
	}else{
		obj.addClass("isChange");
		obj.text("保存");
		objOuter.find(".xkpt-lesson").addClass("weui_cells_access");
		objOuter.find(".xkpt-lesson-input").addClass("on");
	}

}

</script>
</html>